﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class OrderDetailDal
    {
        #region OrderDetailDal private
        private static void SetInfo(IDataRecord reader, ref OrderDetailEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<OrderDetailEntity> orderDetailList)
        {
            while (reader.Read())
            {
                var o = new OrderDetailEntity();
                SetInfo(reader, ref o);
                orderDetailList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static void Create(OrderDetailEntity o)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "OrderDetail_Create", 
                    o.OrderId, o.ParentId, o.CategoryId, o.ProductId, o.Quantity, o.UnitPrice, o.Discount);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "OrderDetail_Create: {0}", ex.Message));
            }
        }

        public static void Delete(int id)
        {
            try
            {
                if (id > 0)
                    SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "OrderDetail_Delete", id);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "OrderDetail_Delete: {0}", ex.Message));
            }
        }

        public static List<OrderDetailEntity> GetListByOrderId(int orderId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstOrderDetail = new List<OrderDetailEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "OrderDetail_GetListByOrderId", orderId);
                SetInfoList(ref reader, ref lstOrderDetail);
                return lstOrderDetail;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "OrderDetail_GetListByOrderId: {0}", ex.Message));
            } 
        }

        public static void DeleteAllByOrderId(int orderId)
        {
            try
            {
                if (orderId > 0)
                    SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "OrderDetail_DeleteAll_ByOrderId", orderId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "OrderDetail_DeleteAll_ByOrderId: {0}", ex.Message));
            }
        }
    }
}
